package com.example.fresheveryday.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.fresheveryday.po.Product;
import com.example.fresheveryday.po.Productorder;
import com.example.fresheveryday.po.Suborder;
import com.example.fresheveryday.restful.JsonResult;
import com.example.fresheveryday.service.ProductorderService;
import com.example.fresheveryday.service.SuborderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ll
 * @since 2023-07-5
 */
@RestController
@RequestMapping("/productorder")
public class ProductorderController {
    @Autowired
    ProductorderService productorderService;
    @Autowired
    SuborderService suborderService;
    @RequestMapping("/myList")//获取当前用户的订单
    public List<Productorder> getMyOrder(String id){
        QueryWrapper<Productorder> wrapper = new QueryWrapper<>();
        wrapper.eq("userid",id);

        return productorderService.list(wrapper);
    }
    @RequestMapping("/createOrder")
    public String create(@RequestBody(required = false)  Map<String,Object> map){
        Object order = map.get("productorder");
        Productorder productorder = JSON.parseObject(JSON.toJSONString(order),Productorder.class);
        String str1 = map.get("id").toString();
        String str2 = str1.substring(1);
        String idsStr = str2.substring(0,str2.length() - 1);
        String idsStrArray[] = idsStr.replace(" ","").split(",");
        int id[] = new int[idsStrArray.length];
        for(int i = 0 ; i< idsStrArray.length;i++){
            id[i] = Integer.parseInt(idsStrArray[i]);
        }
        System.out.println(id);
        productorder.setOrderid(null);
        System.out.println(productorder);
        return productorderService.createOrder(productorder, id);
    }
    @RequestMapping("getOrder")
    public Productorder getOrder(  String ordersId){
        return productorderService.getOrdersByOrdersId(ordersId);
    }
    @RequestMapping("toPay")
    public JsonResult toPay(String ordersId){
        Productorder order = productorderService.getOrdersByOrdersId(ordersId);
        order.setState(2);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("orderId",ordersId);
        productorderService.update(order,wrapper);
        return JsonResult.success("success");
    }
    @RequestMapping("getOrdersList")
    public List<Productorder> getOrdersList(String userId){
        List<Productorder> productorders = productorderService.getOrdersList(userId);

        return productorderService.getOrdersList(userId);
    }

    @RequestMapping("getAllOrder")
    public List<Productorder> list(){
        List<Productorder> list =  productorderService.list();

       return productorderService.list();
    }
    @RequestMapping("/getByCondition")
    public IPage<Productorder> getByCondition(@RequestBody Map map){
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if(map.get("orderId")!=""){
            wrapper.eq("orderId",map.get("orderId"));
            System.out.println(map.get("orderId"));
        }
        if(map.get("state")!=""){
            wrapper.eq("state",map.get("state"));
            System.out.println(map.get("state"));
        }


        Page<Productorder> page = new Page<>();
        page.setCurrent(Integer.valueOf(map.get("pageNo").toString()))
                .setTotal(productorderService.countByConditions(wrapper))
                .setSize(Integer.valueOf(map.get("pageSize").toString()));
        return productorderService.getOrderByConditions(page,wrapper);
    }

}
